package study.transaction.jdbc.dao;

import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@Slf4j
public class JdbcUtils {

    private static final String CONN_SQL = StrUtil.builder()
            .append(" SELECT connection_id(); ").toString();


    private static final String TRX_SQL = StrUtil.builder()
            .append(" SELECT trx_id")
            .append(" FROM information_schema.innodb_trx")
            .append(" WHERE trx_mysql_thread_id = connection_id();").toString();

    @SneakyThrows
    public static String getConnId(Connection connection) {
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery(CONN_SQL);
        String value = null;
        while (rs.next()) {
            value = rs.getString(1);
        }
        return value;
    }
}
